import {acceptHMRUpdate, defineStore} from "pinia";
import {onMounted, ref} from "vue";

const __DEFAULT_MENU_KEY = "__DEFAULT_MENU_KEY";
export const useMenuStore = defineStore("menu", () => {
    const selectKey = ref<string[]>(['2']);

    const setMenuCache = () => {
        localStorage.setItem(__DEFAULT_MENU_KEY, JSON.stringify(selectKey.value[0]));
    }

    const getMenuCache = () => {
        let data = localStorage.getItem(__DEFAULT_MENU_KEY);
        if (data) {
            data = JSON.parse(data);
            selectKey.value = (!data || isNaN(Number(data))) ? ["2"] : [data];
        }
    }

    onMounted(() => {
        getMenuCache();
    })
    return {
        selectKey,
        setMenuCache,
    };
});

if (import.meta.hot)
    import.meta.hot.accept(acceptHMRUpdate(useMenuStore, import.meta.hot));
